<template>
    <div>
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  @load="onLoad"
  :immediate-check="false"
>
<article-item v-for='obj in articleList'
        :key="obj.art_id"
        :artObj="obj"></article-item>
</van-list>
</van-pull-refresh>
    </div>
</template>
<script>
import ArticleItem from './components/ArtucleItem'
import { getArtucleListAPI } from '@/api'
import { Toast } from 'vant'
export default {
  props: {
    channelId: Number
  },
  components: {
    ArticleItem
  },
  data () {
    return {
      articleList: [],
      list: [],
      loading: false,
      finished: false,
      nextTime: new Date(),
      refreshing: false
    }
  },
  methods: {
    async onLoad () {
      // const res = await getArtucleListAPI({
      //   channel_id: this.channelId,
      //   thimestamp: this.nextTime
      // })
      // if (res.data.data && res.data.data.results && res.data.data.results.length === 0) {
      //   this.finished = true
      // }
      // this.articleList = [...this.articleList, ...res.data.data.results]
      // this.loading = false
      if (this.articleList.length > 0) {
        this.getArtucleListFn()
      }
    },
    async getArtucleListFn () {
      const res = await getArtucleListAPI({
        channel_id: this.channelId,
        timestamp: this.nextTime
      })
      // if (res.data.data && res.data.data.results && res.data.data.results.length === 0) {
      //   this.finished = true
      // }
      // if (res.data?.data?.results?.length === 0) {
      //   this.finished = true
      // }
      this.articleList = [...this.articleList, ...res.data.data.results]
      if (res.data?.data?.pre_timestamp === null) {
        this.finished = true
        this.nextTime = null
      } else {
        this.nextTime = res.data.data.pre_timestamp
        this.loading = false

        // setTimeout(() => {
        //   this.refreshing = false
        // }, 1000)
      }
      setTimeout(() => {
        this.refreshing = false
        Toast('刷新成功')
      }, 1000)
    },
    onRefresh () {
    // 清空列表数据
    // this.finished = false
      this.articleList = []
      // 重新加载数据
      this.nextTime = new Date()
      this.getArtucleListFn()
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
    }
  },

  created () {
    // const res = await getArtucleListAPI({
    //   channel_id: this.channelId
    // })
    // this.articleList = res.data.data.results
    // this.nextTime = res.data.data.pre_timestamp
    this.getArtucleListFn()
  }
}
</script>
